package com.leetcode.search;

public class SequentialSearchST<K,V> {
    private Node first;
    private class Node{
        K key;
        V val;
        Node next;

        public Node(K key, V val, Node next) {
            this.key = key;
            this.val = val;
            this.next = next;
        }
    }
    public V get(K key){
        for (Node x=first;x!=null;x=x.next)
            if (key.equals(x.key))return x.val;
        return null;
    }
    public void put(K key,V val){
        for (Node x=first;x!=null;x=x.next){
           if (key.equals(x.key)){
               x.val=val;
               return;
           }
        }
        first=new Node(key,val,first);//首插法
    }
    public int size(){
        int size=0;
        for (Node x=first;x!=null;x=x.next){
            size++;
        }
        return size;
    }
    public void delete(K key){
        for (Node x=first;x!=null;x=x.next){
            if (key.equals(x.next.val)){
                Node node=x.next;
                x.next=x.next.next;
                node.next=null;
            }
        }
    }
    public boolean contains(K key){
        return get(key)!=null;
    }
    boolean isEmpty(){
        return size()==0;
    }
}
